import errorCode from '@/utils/axios/errorCode.js';
import { ToastLoading } from '@/utils/comm.js';
/**
 * 显示错误消息
 * @param {object} axios - Axios库实例
 * @param {object} error - 错误对象
 * @returns {void} - 返回错误消息
 */
export const showMessage = (axios, error) => {
  if (axios.isCancel(error)) {
    return console.error('重复请求' + error);
  }
  let message = '';
  let type = 'warning';
  if (error && error.response) {
    if (error.response.status === 404) {
      message = `${errorCode['404'] + error.response.config.url}`;
      type = 'error';
    } else if (error.response.status !== 404) {
      message = `${errorCode[error.response.status]}`;
    } else {
      message = '异常问题，请联系管理员！';
    }
  }
  // 如果出现timeout说明是超时
  if (error.message.includes('timeout')) {
    type = 'error';
    message = `${errorCode[['408']]}`;
  }
  // 说明服务器异常
  if (error.message.includes('Network')) {
    type = 'error';
    message = window.navigator.onLine ? '服务端异常！' : '您断网了！';
  }
  if (error.code === 'ERR_NETWORK') {
    message = `${errorCode[['401']]}`;
  }
  new ToastLoading(message, type);
  return message;
};
